﻿using System;
using System.Windows;
using Blackbird.Common.Utility;
using Blackbird.Core.Editable;
using Blackbird.Core.Editable.Sprites;
using Microsoft.Win32;

namespace Blackbird.Editors
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        public EditableModule Module { get; set; }
        public string ModuleFilename { get; set; }

        public void NewModule(string name)
        {
            Module = new EditableModule
                         {
                             Name = name
                         };
        }

        #region File Menu Events

        private void FileNewModuleMenuItemClick(object sender, RoutedEventArgs e)
        {
            var nmw = new NewItemWindow {ItemType = "New Module", ItemName = "NewModule"};
            bool? response = nmw.ShowDialog();
            if (response.HasValue && response.Value)
            {
                NewModule(nmw.ItemName);
            }
        }

        private void FileSaveModuleMenuItemClick(object sender, RoutedEventArgs e)
        {
            SaveCommand();
        }

        private void FileCloseMenuItemClick(object sender, RoutedEventArgs e)
        {
            Module = null;
        }

        private void FileSaveModuleAsMenuItemClick(object sender, RoutedEventArgs e)
        {
            SaveAsCommand();
        }

        private void FileExitMenutItemClick(object sender, RoutedEventArgs e)
        {
            ExitCommand();
        }

        private void FileNewMultispriteMenuItemClick(object sender, RoutedEventArgs e)
        {
            AddNewSpriteCommand();
        }

        #endregion

        #region Commands

        private void SaveCommand()
        {
            if (Module != null)
            {
                if (String.IsNullOrEmpty(ModuleFilename))
                    SaveAsCommand();
                else
                    SerializationUtility.SaveToFile(ModuleFilename, Module);
            }
        }

        private void SaveAsCommand()
        {
            var saveFile = new SaveFileDialog {Filter = "Blackbird Editable Module Files (*.mdl)|*.mdl"};
            bool? response = saveFile.ShowDialog();
            if (response.Value)
            {
                ModuleFilename = saveFile.FileName;
                SerializationUtility.SaveToFile(ModuleFilename, Module);
            }
        }

        private void ExitCommand()
        {
            var confirmAction = ConfirmAction("Are you sure you want to exit?");
            if (confirmAction != null && confirmAction.Value)
            {
                Close();
            }
        }

        private void AddNewSpriteCommand()
        {
            if (Module != null)
            {
                var nim = new NewItemWindow
                              {
                                  ItemType = "New Sprite",
                                  ItemName = "NewSprite"
                              };
                bool? response = nim.ShowDialog();
                if (response.HasValue && response.Value)
                {
                    Module.SpritePack.Add(new EditableMultisprite
                                              {
                                                  Name = nim.ItemName
                                              });
                }
            }
        }

        private static bool? ConfirmAction(string action)
        {
            MessageBoxResult result = MessageBox.Show(action, "Confirm", MessageBoxButton.YesNoCancel);
            bool? response = null;
            if (result == MessageBoxResult.Yes)
                response = true;
            else if (result == MessageBoxResult.No)
                response = false;
            return response;
        }

        #endregion
    }
}